{

    MyDesktop.split = function(str) {
        if (str) {
            var result = str.toString();

            result = result.substring(0, 30)
            if (str.length > 30) {
                result = result + '...';
            }
            return result.replace('<', '&lt;');
        }
        return str;
    }

    MyDesktop.SanphamManagement = Ext.extend(Ext.app.Module, {
        createWindow : function() {
            var desktop = app.getDesktop();
            var win = desktop.getWindow('listSanpham');
            if (!win) {
                win = desktop.createWindow({
                    id          : 'listSanpham',
                    title       : "<?=$this->translate->_('Sanpham Management')?>",
                    width       : <?=$this->winWidth?>,
                    height      : <?=$this->winHeight?>,
                    iconCls     : 'icon-grid',
                    layout      : 'border',
                    maximizable : false,

                    tbar        : [/*
                                     * { id : 'delete', text : "<?=$this->translate->_('Delete')?>",
                                     * icon : '<?=ICONS_PATH?>/delete16x16.png',
                                     * cls : "x-btn-text-icon", handler :
                                     * function() { // submit code here } }, {
                                     * id : 'newSanpham', text : "<?=$this->translate->_('New
                                     * Sanpham')?>", icon : '<?=ICONS_PATH?>/newuser16x16.png',
                                     * cls : "x-btn-text-icon", handler :
                                     * function() { } },
                                     */{
                            id      : 'refresh',
                            text    : "<?=$this->translate->_('Refresh')?>",
                            icon    : '<?=ICONS_PATH?>/refresh16x16.png',
                            cls     : "x-btn-text-icon",
                            handler : function() {
                                MyDesktop.SanphamManagement.dataStore.reload();
                            }
                        }],

                    items       : [MyDesktop.SanphamManagement.listGrid,
                        MyDesktop.SanphamManagement.formAdd],

                    listeners   : {
                        close : function() {
                        }
                    }

                });
            }
            <?php
            if ($this->maximizeWin) {
            echo "win.maximize();";
            }
            ?>
            win.show();

        }
    });

    /**
     * Define Store
     */
    MyDesktop.SanphamManagement.dataStore = new Ext.data.Store({
        proxy    : new Ext.data.HttpProxy({
                    url    : '<?=BASE_URL?>/<?=$this->currentController?>/list',
                    method : 'POST'
                }),
        reader   : new Ext.data.JsonReader({
                    root          : 'results',
                    totalProperty : 'total',
                    id            : '<?=$this->pk?>'
                }, [{
                        name    : 'SanPhamID',
                        mapping : 'SanPhamID'
                    }, {
                        name    : 'MaSanPham',
                        mapping : 'MaSanPham'
                    }, {
                        name    : 'TenSanPham',
                        mapping : 'TenSanPham'
                    }, {
                        name    : 'SoLuongTrongKho',
                        mapping : 'SoLuongTrongKho'
                    }]),
        sortInfo : {
            field     : '<?=$this->pk?>',
            direction : "DESC"
        }
    });

    /**
     * Define Column Model
     */
    MyDesktop.SanphamManagement.columnModel = new Ext.grid.ColumnModel({
                defaults : {
                    sortable : true
                },
                columns  : [new Ext.grid.RowNumberer(), {
                        header    : 'SanPhamID',
                        dataIndex : 'SanPhamID',
                        hidden    : true
                    }, {
                        header    : 'MaSanPham',
                        dataIndex : 'MaSanPham'
                    }, {
                        header    : 'TenSanPham',
                        dataIndex : 'TenSanPham'
                    }, {
                        header    : 'SoLuongTrongKho',
                        dataIndex : 'SoLuongTrongKho'
                    }]
            });

    /**
     * Define Grid
     */
    MyDesktop.SanphamManagement.listGrid = new Ext.grid.EditorGridPanel({
                id           : 'Sanphamgrid',
                title        : "<?=$this->translate->_('List')?>",
                store        : MyDesktop.SanphamManagement.dataStore,
                cm           : MyDesktop.SanphamManagement.columnModel,
                clicksToEdit : 1,
                region       : 'center',
                width        : 550,
                split        : true,
                selModel     : new Ext.grid.RowSelectionModel({
                            singleSelect : true,
                            listeners    : {
                                rowselect : function(sm, row, rec) {
                                    MyDesktop.SanphamManagement.formAdd
                                            .expand(true);
                                    MyDesktop.SanphamManagement.formAdd
                                            .getForm().loadRecord(rec);
                                }
                            }
                        }),
                listeners    : {
                    render : function() {
                        MyDesktop.SanphamManagement.dataStore.load();
                    }

                }
            });

    MyDesktop.SanphamManagement.formAdd = new Ext.FormPanel({
        id          : 'SanphamFormPanel',
        url         : '<?=BASE_URL?>/<?=$this->currentController?>/save',
        region      : "east",
        width       : 730,
        collapsible : true,
        split       : true,
        defaultType : 'textfield',
        autoScroll  : true,
        frame       : true,
        items       : [{
                fieldLabel : 'SanPhamID',
                name       : 'SanPhamID',
                id         : 'SanPhamID',
                style      : 'width:98%',
                readOnly   : true
            }, {
                fieldLabel : "<?=$this->translate->_('MaSanPham')?>",
                name       : 'MaSanPham',
                id         : 'MaSanPham',
                style      : 'width:98%',
                allowBlank : false
            }, {
                fieldLabel : "<?=$this->translate->_('TenSanPham')?>",
                name       : 'TenSanPham',
                id         : 'TenSanPham',
                style      : 'width:98%',
                allowBlank : false
            }, {
                fieldLabel : "<?=$this->translate->_('SoLuongTrongKho')?>",
                name       : 'SoLuongTrongKho',
                id         : 'SoLuongTrongKho',
                style      : 'width:98%'
            }],
        tbar        : [{
            id      : "submitFormEditSanpham",
            text    : '<?=$this->translate->_("Save")?>',
            icon    : '<?=ICONS_PATH?>/save.gif',
            tooltip : '<?=$this->translate->_("Save to database")?>',
            handler : function() { // submit code here
                var form = MyDesktop.SanphamManagement.formAdd.form;
                form.submit({
                    waitMsg : '<?=$this->translate->_("Please wait")?>',
                    reset   : false,
                    success : function(f, a) {
                        // form.toBack();
                        var message = "<?=$this->translate->_('Can\'t save to DB!')?>";
                        try {
                            message = a.result.message;
                        } catch (ex) {
                        }
                        MyDesktop.SanphamManagement.dataStore.reload();
                        Ext.Msg.alert('<?=$this->translate->_("Status")?>',
                                message);
                    },
                    failure : function(f, a) {
                        // form.toBack();
                        var message = "<?=$this->translate->_('Your form is not valid!')?>";
                        try {
                            message = a.result.message;
                        } catch (ex) {
                        }
                        Ext.Msg.alert('<?=$this->translate->_("Status")?>',
                                message);
                    }
                });
            }
        }, {
            text    : '<?=$this->translate->_("Form Add")?>',
            icon    : '<?=ICONS_PATH?>/refresh16x16.png',
            cls     : "x-btn-text-icon",
            handler : function() {
                var form = MyDesktop.SanphamManagement.formAdd;

                form.getForm().reset();
            }

        }, {
            id      : 'Delete',
            text    : "<?=$this->translate->_('Delete')?>",
            icon    : '<?=ICONS_PATH?>/delete16x16.png',
            cls     : "x-btn-text-icon",
            handler : function() { // submit code here
                var form = MyDesktop.SanphamManagement.formAdd;

                var id = form.findById('<?=$this->pk?>').getValue();

                Ext.Ajax.request({
                    url     : '<?=BASE_URL?>/<?=$this->currentController?>/delete/id/'
                            + id,
                    success : function(r) {
                        MyDesktop.SanphamManagement.dataStore.reload();
                        Ext.Msg
                                .alert('<?=$this->translate->_("Status")?>',
                                        '<?=$this->translate->_("Delete successfull!")?>');
                        form.getForm().reset();
                    },
                    failure : function(r) {
                        Ext.Msg
                                .alert('<?=$this->translate->_("Status")?>',
                                        '<?=$this->translate->_("Request Fail! Please check your connection")?>');
                    }
                });
            }
        }]
    });

    var SanphamManagement = new MyDesktop.SanphamManagement();
    SanphamManagement.createWindow();

}